<?php
/**
 * 部门管理
 *@date 2012-1-5
 *@author zhouyt
*/

class Sys_DepartmentController extends Zend_Controller_Action
{
	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Base();

		if(!$this->request->isXmlHttpRequest()) {
			$this->common->PutData('{result:"",error:"非法的请求，请重试或与管理员联系..."}');
		}
        /* Initialize action controller here */
    }

	public function getlistAction()
	{
		$start = $this->request->getParam("start");
		$limit = $this->request->getParam("limit");
			$_iAmount = $this->db->fetchOne("SELECT count(departmentid) FROM department WHERE status & 1 = 0");
			$result = $this->db->fetchAll("SELECT departmentid, departname, optionset, status, remark, createon FROM department WHERE status & 1 = 0 limit $start, $limit");
			$sMsg = "";
			$len = count($result);
			for($i=0; $i < $len; $i++) {
				$comma = ",";
				if($i == ($len -1)) 
				{
					$comma = "";
				}
				$sMsg .= "{DepartmentID:" . $result[$i]["departmentid"] . ",DepartName:'" . $result[$i]["departname"] . "',Remark:'" . $result[$i]["remark"] . "',OptionSet:" . $result[$i]["optionset"] . ",CreateOn:'" . $result[$i]["createon"] . "',Status:'" . $result[$i]["status"] . "',LockStatus: " . $result[$i]["status"] . "}" . $comma;
			}

			$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
			$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
                                  ",aList:" . $acl["aList"] . "}]";
			$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
			exit;
		
	}
	public function namelistAction()
	{
		$result = $this->db->fetchAll("SELECT departmentid, departname FROM department WHERE status & 1 = 0");
        
		$sMsg = "";
		$len = count($result);
		$_iAmount = $len; 
		for($i=0; $i < $len; $i++)
        {
			$comma = ",";
			if($i == ($len -1)) 
			{
				$comma = "";
			}
			$sMsg .= "{DepartmentID:" . $result[$i]["departmentid"] . ",DepartmentName:'" . $result[$i]["departname"] . "'}" . $comma;
        }

		$this->common->PutData("{totalProperty:" . $_iAmount . ",records:[" . $sMsg . "]}");
		exit;
	}

	public function saveAction()
	{
		$departmentid = $this->request->getParam("DepartmentID");
		$departname = $this->request->getParam("DepartName");
		$remark = $this->request->getParam("Remark");
		
		if($departmentid > 0) {
			if($this->db->update("department", array(
					"departname" => $departname,
					"remark"    => $remark
				), $this->db->quoteInto('departmentid = ?', $departmentid)))
				{
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"保存失败!"}');
				}
		}
		else
		{
			if($this->db->insert("department", array(
				"departname" => $departname,
				"remark"    => $remark,
				"optionset" => 0,
				"status"    => 0,
				"createon"  => $this->common->getGmtime()
			))) {
				
				$this->common->PutData('{result:""}');
			}
			else
			{
				$this->common->PutData('{error:"保存失败!"}');
			}
		}
		
		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("department", array(
					"status" => 1
				), $this->db->quoteInto('departmentid in (?)', $uids))) 
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE department SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('departmentid in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}
}
